package artibella.media.controls.ui 
{
	import artibella.IDisposable;
	import artibella.media.controls.AController;
	import artibella.media.controls.AudioController;
	
	import flash.display.JointStyle;
	import flash.display.LineScaleMode;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	/**
	 * ...
	 * @author pbordachar
	 */
	
	public class VolumeSlider extends MovieClip implements IDisposable
	{
		private var _controller:AController;
		
		private var _clr:int;
		
		private var _hz:Sprite;
		private var _niveau:Sprite;
		
		public function VolumeSlider( ctr:AController, c:int = 0x000000 ) 
		{
			_controller = ctr;
			_clr = c;
			
			_init();
		}
		
		// - - -
		
		private function _init():void
		{
			graphics.lineStyle( 1, _clr, 1, false, LineScaleMode.NONE, null, JointStyle.MITER );
			graphics.moveTo( -7, 3 );
			graphics.lineTo( -7, 1 );
			graphics.moveTo( -4, 3 );
			graphics.lineTo( -4, 0 );
			graphics.moveTo( -1, 3 );
			graphics.lineTo( -1, -1 );
			graphics.moveTo( 2, 3 );
			graphics.lineTo( 2, -2 );
			graphics.moveTo( 5, 3 );
			graphics.lineTo( 5, -3 );
			graphics.moveTo( 8, 3 );
			graphics.lineTo( 8, -4 );
			
			//graphics.lineStyle( 1, 0xFFFFFF, 1, true, LineScaleMode.NONE, null, JointStyle.ROUND );
			//graphics.moveTo( -8, 2 );
			//graphics.lineTo( -7, 2 );
			//graphics.moveTo( -5, 2 );
			//graphics.lineTo( -4, 2 );
			//graphics.moveTo( -2, 2 );
			//graphics.lineTo( -1, 2 );
			//graphics.moveTo( 1, 2 );
			//graphics.lineTo( 2, 2 );
			//graphics.moveTo( 4, 2 );
			//graphics.lineTo( 5, 2 );
			//graphics.moveTo( 7, 2 );
			//graphics.lineTo( 8, 2 );
			
			_niveau = new Sprite();
			_niveau.graphics.beginFill( _clr, 1 );
			_niveau.graphics.drawRect( 0, 0, 20, 10 );
			_niveau.graphics.endFill();
			_niveau.x = -8;
			_niveau.y = -5 ;
			addChild( _niveau );
			
			var m:Sprite = new Sprite();
			m.graphics.lineStyle();
			m.graphics.beginFill( 0xFF0000, 1 );
			m.graphics.drawRect( -8, 1, 2, 2 );
			m.graphics.drawRect( -5, 0, 2, 3 );
			m.graphics.drawRect( -2, -1, 2, 4 );
			m.graphics.drawRect( 1, -2, 2, 5 );
			m.graphics.drawRect( 4, -3, 2, 6 );
			m.graphics.drawRect( 7, -4, 2, 7 );
			m.graphics.endFill();
			addChild( m );
			
			_niveau.mask = m;
			
			_hz = new Sprite();
			_hz.graphics.beginFill( _clr, 0 );
			_hz.graphics.drawRect( 0, 0, 20, 10 );
			_hz.graphics.endFill();
			_hz.x = -10;
			_hz.y = -5 ;
			addChild( _hz );
			
			_hz.buttonMode = true;
			_hz.addEventListener( MouseEvent.MOUSE_DOWN, _doVolume );
		}
		
		// - - -
		
		private function _doVolume( e:MouseEvent ):void
		{
			_hz.removeEventListener( MouseEvent.MOUSE_DOWN, _doVolume );
			
			_hz.addEventListener( Event.ENTER_FRAME, _slideVolume );
			stage.addEventListener( MouseEvent.MOUSE_UP, _doVolumeDone );
		}
		
		private function _doVolumeDone( e:MouseEvent ):void
		{
			stage.removeEventListener( MouseEvent.MOUSE_UP, _doVolumeDone );
			
			_hz.removeEventListener( Event.ENTER_FRAME, _slideVolume );
			_hz.addEventListener( MouseEvent.MOUSE_DOWN, _doVolume );
		}
		
		private function _slideVolume( e:Event ):void
		{
			var position:int;
			
			if ( _hz.mouseX < 0 ) {
				position = 0;
			} else if ( _hz.mouseX > _hz.width ){
				position = _hz.width;
			} else {
				position = _hz.mouseX;
			}
			
			var volume:Number = position / _hz.width ;
			
			_niveau.width = volume;
			
			_controller.setVolume( volume );
		}
		
		// - - -
		
		public function volUpdate( n:Number ):void
		{
			_niveau.width = n * _hz.width; 
		}
		
		// - - -
		
		public function dispose():void
		{
			_controller = null;
		}
	}
}